package com.py.base.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;

import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


public class ExcelUtil {

    /**
     * 导出excel
     * @param response 相应response
     * @param title    保存文件的标题
     * @param head     保存excel对象的实体类
     * @param list     需要保存的数据列表
     * @throws IOException  异常捕获
     */
    public static void exportExcel(HttpServletResponse response, String title, Class head, List list) throws IOException {
        exportExcel(response, title, head, list, null);

    }

    public static void exportExcel(HttpServletResponse response, String title, Class head, List list, Set<String> set) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(title, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        if(set == null){
            EasyExcel.write(response.getOutputStream(), head).sheet("模板")
                    .doWrite(list);
        }else{
            EasyExcel.write(response.getOutputStream(), head).includeColumnFiledNames(set).sheet("模板")
                    .doWrite(list);
        }

    }
    public static void writeExcel(File newFile, List<String> headerList, List<List<Object>> datalist, int type) {
        //初始化excel
        ExcelWriter excelWriter = EasyExcel.write(newFile).build();
        //获取阀门名称
        List<String> valveNameList = new ArrayList<>();
        for (List<Object> objectList : datalist) {
            valveNameList.add((String) objectList.get(0));
        }
        //去重
        List<String> collectList = valveNameList.stream().distinct().collect(Collectors.toList());
        //按阀门创建sheet
        // 创建一个表格
        if (type == 1) {
            //多sheet
            //创建表头
            List<List<String>> headList = new ArrayList<>();
            // 第 n 行 的表头
            for (String s : headerList) {
                List<String> headTitle = new ArrayList<>();
                headTitle.add(s);
                headList.add(headTitle);
            }
            WriteTable writeTable = EasyExcel.writerTable(0).head(headList).needHead(true).build();
            String ifValveName;
            for (int i = 0; i < collectList.size(); i++) {
                //创建新数据集合
                List<List<Object>> newList = new ArrayList<>();
                for (List<Object> objects : datalist) {
                    //取出阀门名称
                    ifValveName = (String) objects.get(0);
                    //获取相符合的数据
                    if (collectList.get(i).equals(ifValveName)) {
                        newList.add(objects);
                    }
                }
                WriteSheet writeSheet = EasyExcel.writerSheet(i, collectList.get(i)).build();
                excelWriter.write(newList, writeSheet, writeTable);
            }
        } else {
            //多表头
            String ifValveName;
            for (int i = 0; i < collectList.size(); i++) {
                List<List<Object>> newList = new ArrayList<>();
                for (List<Object> objects : datalist) {
                    ifValveName = (String) objects.get(0);
                    if (collectList.get(i).equals(ifValveName)) {
                        newList.add(objects);
                    }
                }
                //创建表头
                // 第 n 行 的表头
                List<List<String>> headList = new ArrayList<>();
                for (String s : headerList) {
                    List<String> headTitle = new ArrayList<>();
                    headTitle.add(s);
                    headList.add(headTitle);
                }
                WriteTable writeTable = EasyExcel.writerTable(i).head(headList).needHead(true).build();
                WriteSheet writeSheet = EasyExcel.writerSheet(0, "sheet1").build();
                excelWriter.write(newList, writeSheet, writeTable);
            }
        }

        excelWriter.finish();
    }
}
